package com.cn.service;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.cn.dao.CategoryDao;
import com.cn.domain.Book;
import com.cn.domain.Category;
import com.cn.domain.PageBean;

@Transactional
public class CategoryService {
	//注入Dao
	private CategoryDao categoryDao;

	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	public List<Category> findAllCategory() {
		
		return  categoryDao.findAllCategory();
	}

  
    public Category getCategoryId(int categoryid) {
	
		return categoryDao.getCategoryId(categoryid);
	}

	public PageBean<Book> getCategoryId(int categoryid, int page) {
	PageBean<Book> pageBean = new PageBean<Book>();
    pageBean.setPage(page);//设置当前页
    int limitpage =3;//每页显示三条
    pageBean.setLimtpage(limitpage);
    //查询的总记录数
    int totalcount = 0;
    totalcount = categoryDao.findCountCategoryBook(categoryid);//该分类下的书的总记录
    pageBean.setTotalcount(totalcount);
    //总页数
    int totalpage = 0 ;
    if(totalcount%limitpage==0){
    	totalpage = totalcount/limitpage;
    	
    }else{
    	totalpage = totalcount/limitpage+1;
    }
    pageBean.setTotalpage(totalpage);
    //每页显示数据的集合
    int begin = (page-1)*limitpage;
    List<Book>  listbook = categoryDao.findPageCateogry(categoryid,begin,limitpage);
	pageBean.setList(listbook);
    return pageBean;
	}

	public void deleteCategory(int categoryid) {
		categoryDao.deleteCategory(categoryid);
		
	}


	public void updateCategory(Category category) {
		
		categoryDao.updateCategory(category);
		
	}

	public void addCategory(Category category) {
		
		categoryDao.addCategory(category);
		
	}


	

}
